package com.wushi.lenovo.asia5b.app_ui.profile.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.mvp.base.BaseMvpActivity;
import com.mvp.base.model.ResultModel;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_config.CommonConstant;
import com.wushi.lenovo.asia5b.app_di.APPComponentFactory;
import com.wushi.lenovo.asia5b.app_di.profile.mvpview.CheckEmailActivityView;
import com.wushi.lenovo.asia5b.app_di.profile.presenter.CheckEmailActivityPresenter;
import com.wushi.lenovo.asia5b.app_event.ChangeProfileEvent;
import com.wushi.lenovo.asia5b.app_model.profile.bean.ChangeProfileType;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by zhangyuncai on 2018/12/8.
 * 邮箱检验
 */
public class CheckEmailActivity extends BaseMvpActivity<
        CheckEmailActivityView, CheckEmailActivityPresenter> implements CheckEmailActivityView {
    @BindView(R.id.et_resetpass_email)
    EditText et_resetpass_email;
    @BindView(R.id.bt_resetpass_submit)
    Button bt_resetpass_submit;
    @BindView(R.id.bt_resetpass_back)
    Button bt_resetpass_back;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_checkemail;
    }

    @Override
    protected void constructComponent() {
        APPComponentFactory.getAppBaseComponent().inject(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);

        initView();

        initData();

        initEvent();
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }
    private void submit() {
        String email = et_resetpass_email.getText().toString().trim();
        getPresenter().checkEmail(email);
    }

    @Override
    protected void initEvent() {

    }

    @OnClick({
            R.id.bt_resetpass_back,
            R.id.bt_resetpass_submit
    })
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_resetpass_back:
                finish();
                break;
            case R.id.bt_resetpass_submit:
                submit();
                break;
        }
    }

    @Override
    public void checkEmailResult(ResultModel<String> resultModel, String email) {
        if (resultModel.isSuccess()) {
            Intent intent = new Intent(this, VerifyActivity.class);
            intent.putExtra(CommonConstant.RESET_LOGIN_PASS_EMAIL, email);
            intent.putExtra(CommonConstant.RESET_LOGIN_PASS_MOBILE, resultModel.getData());
            startActivity(intent);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void onChangeProfileEvent(ChangeProfileEvent event) {
        if(event!=null&&event.changeProfileType==ChangeProfileType.ChangeLoginPass) {
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
